# common functions

import pygame
import math
import os

fps = pygame.time.Clock() # creates a clock for limiting FPS

path =[(0,296),(104,296),(104,344),(40,344),(40,456),(152,456),(152,296),(232,296),(232,472),(280,472),(280,168),(120,168),(120,248),(56,248),(56,8),(136,8),(136,88),(216,88),(216,8),(296,8),(296,88),(376,88),(376,8),(424,8),(424,136),(328,136),(328,248),(504,248 ),(504,168),(360,168),(360,216),(472,216),(472,88),(584,88),(584,312),(568,312),(568,328),(536,328),(536,344),(504,344),(504,360),(488,360),(488,392),(328,392),(328,328),(408,328),(408,456),(568,456),(568,552),(88,552)]

images = {None:None}
imagedir = "images/"

def load_images():
    for filename in os.listdir(imagedir):
        #if filename[-4:] == ".png":
        try:
        	images[filename[:-4]]=pygame.image.load(imagedir+filename).convert_alpha()
        except:
            print("Failed to load "+imagedir+filename)

            
def test_posrect(pos,rect):
    if pos[0] >= rect[0] and pos[0] <= rect[0]+rect[2] and pos[1] >= rect[1] and pos[1] <= rect[1]+rect[3]:
        return True
        
def get_dist_sqr(x,y):
    dist = x**2 + y**2
    return dist
    
def get_dist(x,y):
    dist = math.sqrt(x**2 + y**2)
    return dist  
     
def get_angle(x,y):
    return math.atan2(x,y)
    
def rad_deg(angle):
    return (angle*180)/math.pi 
    
def get_xy(a,d):
    x=math.sin(a)*-d
    y=math.cos(a)*-d
    return x,y

def sort_health(glist):
    glist.sort(lambda a,b: int((a.health - b.health)*1000))
    return glist
    
def sort_dist(tower, glist):
    
    glist.sort(lambda a,b: int(get_dist(tower.x-a.x,tower.y-a.y)*1000) - int(get_dist(tower.x-b.x,tower.y-b.y))*1000)
    return glist
    
def sort_speed(glist):
    glist.sort(lambda a,b: int((a.speed - b.speed)*1000))
    return glist
    
def get_keystone(glist):
    x,y = 0,0
    for obj in glist:
        x += obj.x
        y += obj.y
    x /= len(glist)
    y /= len(glist)
    
    glist.sort(lambda a,b: int(get_dist_sqr(x-a.x,y-a.y)*1000) - int(get_dist_sqr(x-b.x,y-b.y))*1000)
    
    return glist[0]
    

def get_pointlineseg(linestart,lineend,point):
    """Will only return a point if it is on the linesegment"""
    x1,y1=linestart
    x2,y2=lineend
    x3,y3=point
    u=((x3-x1)*(x2-x1) + (y3-y1)*(y2-y1))/abs(get_dist_sqr(x2-x1,y2-y1)*1.0)    #This works, don't fuck with it.
    if u>=0 and u<=1:
        ix=u*(x2-x1)+x1 #Intersection x
        iy=u*(y2-y1)+y1 #Intersection y
        return (ix,iy)
    else:
        return None

def test_circleline(linestart,lineend,point,radius):
    """returns the magnitude and angle a circle is overlapping a line"""
    target=get_pointlineseg(linestart,lineend,point)
    if target is not None:
        if test_circlecircle(point,radius,target):
            return test_circlecircle2(point, radius, target)
    return False

def test_circlecircle(point1,rad1,point2,rad2=0):
    """Quickly tests if the distance^2 between two circles is less than their radius^2."""
    if get_dist_sqr(point1[0]-point2[0],point1[1]-point2[1])<(rad1+rad2)*(rad1+rad2):
        return True
    else: return False

def test_circlecircle2(point1,rad1,point2,rad2=0):
    """Tests if the distance between two circles is less than their radius. Returns a vector"""
    dist=get_dist(point1[0]-point2[0],point1[1]-point2[1])
    if dist<(rad1+rad2):
        angle=get_angle(point1[0]-point2[0],point1[1]-point2[1])

        return True#(angle,(rad1+rad2)-dist)
    else: return False